<template>
  <el-dialog
    :title="!dataForm.id ? '新增模型' : '修改模型'"
    :close-on-click-modal="false"
    width="80%"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="模型名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="模型名称"  v-bind:readonly="signreadonly"></el-input>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input v-model="dataForm.source" placeholder="来源"></el-input>
      </el-form-item>
      <el-form-item class="bpmn-container1">
<template>
  <div class="diagram-container">
    <bpmn-modeler ref="bpmref"
      :bpmnxml="dataForm.xmlinfo"
      v-on:error="handleError"
      v-on:shown="handleShown"
    ></bpmn-modeler>
  </div>
</template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import BpmnModeler from '@/components/bpmnComponents/bpm-modeler.vue'
  export default {
    data () {
      return {
        visible: false,
        signreadonly: true,
        dataForm: {
          id: 0,
          name: '',
          source: '',
          xmlinfo: ''
        },
        dataRule: {
          name: [
            { required: true, message: '模型名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      BpmnModeler
    },
    methods: {
      handleError: function (err) {
        console.error('failed to show diagram', err)
      },
      handleShown: function () {
        console.log('diagram shown')
      },
      init (id) {
        this.signreadonly = false
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/flow/deployment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.source = data.data.source
                this.dataForm.xmlinfo = data.data.bpmnXml
                this.signreadonly = true
              } else {
                this.dataForm.name = ''
                this.dataForm.source = ''
                this.dataForm.xmlinfo = '<?xml version="1.0" encoding="UTF-8"?>\n' +
                        '<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd">\n' +
                        '  <process id="Process_1ape15a" />\n' +
                        '  <bpmndi:BPMNDiagram id="sid-74620812-92c4-44e5-949c-aa47393d3830">\n' +
                        '    <bpmndi:BPMNPlane id="sid-cdcae759-2af7-4a6d-bd02-53f3352a731d" bpmnElement="Process_1ape15a" />\n' +
                        '    <bpmndi:BPMNLabelStyle id="sid-e0502d32-f8d1-41cf-9c4a-cbb49fecf581">\n' +
                        '      <omgdc:Font name="Arial" size="11" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />\n' +
                        '    </bpmndi:BPMNLabelStyle>\n' +
                        '    <bpmndi:BPMNLabelStyle id="sid-84cb49fd-2f7c-44fb-8950-83c3fa153d3b">\n' +
                        '      <omgdc:Font name="Arial" size="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />\n' +
                        '    </bpmndi:BPMNLabelStyle>\n' +
                        '  </bpmndi:BPMNDiagram>\n' +
                        '</definitions>'
                this.signreadonly = false
              }
            })
          } else {
            this.dataForm.name = ''
            this.dataForm.source = ''
            this.dataForm.xmlinfo = '<?xml version="1.0" encoding="UTF-8"?>\n' +
                        '<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd">\n' +
                        '  <process id="Process_1ape15a" />\n' +
                        '  <bpmndi:BPMNDiagram id="sid-74620812-92c4-44e5-949c-aa47393d3830">\n' +
                        '    <bpmndi:BPMNPlane id="sid-cdcae759-2af7-4a6d-bd02-53f3352a731d" bpmnElement="Process_1ape15a" />\n' +
                        '    <bpmndi:BPMNLabelStyle id="sid-e0502d32-f8d1-41cf-9c4a-cbb49fecf581">\n' +
                        '      <omgdc:Font name="Arial" size="11" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />\n' +
                        '    </bpmndi:BPMNLabelStyle>\n' +
                        '    <bpmndi:BPMNLabelStyle id="sid-84cb49fd-2f7c-44fb-8950-83c3fa153d3b">\n' +
                        '      <omgdc:Font name="Arial" size="12" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />\n' +
                        '    </bpmndi:BPMNLabelStyle>\n' +
                        '  </bpmndi:BPMNDiagram>\n' +
                        '</definitions>'
            this.signreadonly = false
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        let Base64 = require('js-base64').Base64
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/flow/deployment/deploy'),
              method: 'post',
              data: this.$http.adornData({
                'name': this.dataForm.name,
                'source': this.dataForm.source,
                'data': Base64.encode(this.$refs.bpmref.getBpmnXnl())
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style scoped>
  .diagram-container{
    position: absolute;
    width: 100%;
    height: 100%;
  }
  .bpmn-container1 {
    height: 420px;
  }
</style>
